<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\dynamic\controller;

class IndexDynamicList extends \app\ApiRest
{
    protected $order_model;
    protected $model;
    protected $cap_info;
    protected $list_model;
    protected $commment_model;
    protected $thumbs_model;
    protected $config_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\Coach();
        $this->order_model = new \app\massage\model\Order();
        $this->list_model = new \app\dynamic\model\DynamicList();
        $this->commment_model = new \app\dynamic\model\DynamicComment();
        $this->thumbs_model = new \app\dynamic\model\DynamicThumbs();
        $this->config_model = new \app\massage\model\MassageConfig();
    }
    public function dynamicList()
    {
        $input = $this->_param;
        $dis[] = ["a.status", "=", 2];
        $dis[] = ["b.status", "=", 2];
        if(!empty($input["coach_name"])) {
            $dis[] = ["b.coach_name", "like", "%" . $input["coach_name"] . "%"];
        }
        if($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $dis[] = ["b.industry_type", "<>", 1];
        }
        if(!empty($input["coach_id"])) {
            $dis[] = ["a.coach_id", "=", $input["coach_id"]];
        }
        if($this->getUserId()) {
            $shield_model = new \app\massage\model\ShieldList();
            $coach_id = $shield_model->where(["user_id" => $this->getUserId()])->column("coach_id");
            $dis[] = ["a.coach_id", "not in", $coach_id];
        }
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((a.lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((a.lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (a.lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
        $data = $this->list_model->coachDataList($dis, $alh);
        if($data["data"]) {
            $dynamic_id = \app\dynamic\model\DynamicThumbs::where(["user_id" => $this->getUserId(), "status" => 1])->column(["dynamic_id"]);
            foreach ($data["data"] as &$datum) {
                $datum["is_thumbs"] = in_array($datum["id"], $dynamic_id) ? 1 : 0;
            }
        }
        return $this->success($data);
    }
    public function followDynamicList()
    {
        $input = $this->_param;
        $dis[] = ["a.status", "=", 2];
        $dis[] = ["b.status", "=", 2];
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["c.status", "=", 1];
        $dis[] = ["a.user_id", "=", $this->_user_id];
        if($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $dis[] = ["b.industry_type", "<>", 1];
        }
        if(!empty($input["coach_name"])) {
            $dis[] = ["b.coach_name", "like", "%" . $input["coach_name"] . "%"];
        }
        $shield_model = new \app\massage\model\ShieldList();
        $coach_id = $shield_model->where(["user_id" => $this->_user_id])->column("coach_id");
        $dis[] = ["a.coach_id", "not in", $coach_id];
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((a.lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((a.lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (a.lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
        $data = $this->list_model->coachFollowDataList($dis, $alh);
        $follow_model = new \app\dynamic\model\DynamicFollow();
        $record = $follow_model->getFollowDynamicData($this->_user_id);
        if(!empty($record)) {
            foreach ($record as $k => $value) {
                $insert[$k] = ["uniacid" => $value["uniacid"], "user_id" => $this->_user_id, "dynamic_id" => $value["id"]];
            }
            $record_model = new \app\dynamic\model\DynamicWatchRecord();
            $record_model->saveAll($insert);
        }
        return $this->success($data);
    }
    public function dynamicInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $this->list_model->where($dis)->update(["pv" => \think\facade\Db::Raw("pv+1")]);
        $data = $this->list_model->dataInfo($dis);
        if(empty($data)) {
            $this->errorMsg("动态被删除");
        }
        $data["coach_info"] = $this->model->where(["id" => $data["coach_id"]])->field("industry_type,coach_name,work_img,is_work,work_time,status")->find();
        if(empty($data["coach_info"])) {
            $this->errorMsg("技师已下架");
        }
        $type_model = new \app\industrytype\model\Type();
        $data["coach_info"]["industry_data"] = $type_model->dataInfo(["id" => $data["coach_info"]["industry_type"]], "employment_years");
        $data["coach_info"]["work_status"] = $this->model->getCoachWorkStatus($data["coach_id"], $this->_uniacid);
        $data["distance"] = distance_text(getdistances($data["lng"], $data["lat"], $lng, $lat));
        $follow_model = new \app\dynamic\model\DynamicFollow();
        $find = $follow_model->dataInfo(["coach_id" => $data["coach_id"], "user_id" => $this->_user_id, "status" => 1]);
        $data["follow_status"] = !empty($find) ? 1 : 0;
        $find = $this->thumbs_model->dataInfo(["dynamic_id" => $data["id"], "user_id" => $this->_user_id, "status" => 1]);
        $data["thumbs_status"] = !empty($find) ? 1 : 0;
        $dis = ["a.id" => $input["id"]];
        $data["comment_num"] = getFriendNum($this->list_model->getCommentNum($dis));
        $data["thumbs_num"] = getFriendNum($this->list_model->getThumbsNum($dis));
        $shield_model = new \app\massage\model\ShieldList();
        $coach_id = $shield_model->where(["user_id" => $this->_user_id])->column("coach_id");
        $data["is_shield"] = in_array($data["coach_id"], $coach_id) ? 1 : 0;
        return $this->success($data);
    }
    public function commentList()
    {
        $input = $this->_param;
        $dis[] = ["a.dynamic_id", "=", $input["dynamic_id"]];
        $where[] = ["a.status", "=", 2];
        $id_arr = $this->commment_model->where(["user_id" => $this->_user_id, "dynamic_id" => $input["dynamic_id"]])->where("status", "in", [1, 2])->column("id");
        $where[] = ["a.id", "in", $id_arr];
        $data = $this->commment_model->getCommentList($dis, 10, $where);
        $add_user = $this->list_model->where(["id" => $input["dynamic_id"]])->value("user_id");
        if(!empty($data)) {
            foreach ($data["data"] as &$v) {
                $v["del_auth"] = $add_user == $this->_user_id || $v["user_id"] == $this->_user_id ? 1 : 0;
            }
        }
        return $this->success($data);
    }
    public function commentAdd()
    {
        $input = $this->_input;
        $config = $this->config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $dynamic = $this->list_model->dataInfo(["id" => $input["dynamic_id"]]);
        if(empty($dynamic)) {
            $this->errorMsg("动态已被删除");
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->_user_id, "dynamic_id" => $input["dynamic_id"], "text" => $input["text"], "coach_id" => $dynamic["coach_id"], "status" => $config["dynamic_comment_check"]];
        $setting = new \longbingcore\wxcore\WxSetting($this->_uniacid);
        $check = $setting->checkKeyWords(trim($input["text"]));
        if(!$check) {
            $this->errorMsg("输入文字内容含有敏感违禁词");
        }
        $res = $this->commment_model->dataAdd($insert);
        return $this->success(["status" => $insert["status"]]);
    }
    public function commentDel()
    {
        $input = $this->_input;
        $res = $this->commment_model->dataUpdate(["id" => $input["id"]], ["status" => -1]);
        return $this->success($res);
    }
    public function followAddOrCancek()
    {
        $input = $this->_input;
        $input["dynamic_id"] = empty($input["dynamic_id"]) ? 0 : $input["dynamic_id"];
        $follow_model = new \app\dynamic\model\DynamicFollow();
        $find = $follow_model->dataInfo(["coach_id" => $input["coach_id"], "user_id" => $this->_user_id]);
        if(!empty($find) && $find["status"] == 1) {
            $res = $follow_model->dataUpdate(["id" => $find["id"]], ["status" => -1]);
        } elseif(!empty($find)) {
            $res = $follow_model->dataUpdate(["id" => $find["id"]], ["status" => 1, "create_time" => time(), "dynamic_id" => $input["dynamic_id"], "have_look" => 0]);
        } else {
            $insert = ["uniacid" => $this->_uniacid, "coach_id" => $input["coach_id"], "user_id" => $this->_user_id, "dynamic_id" => $input["dynamic_id"]];
            $res = $follow_model->dataAdd($insert);
        }
        return $this->success($res);
    }
    public function thumbsAddOrCancek()
    {
        $input = $this->_input;
        $dynamic = $this->list_model->dataInfo(["id" => $input["dynamic_id"]]);
        if(empty($dynamic)) {
            $this->errorMsg("动态已被删除");
        }
        $find = $this->thumbs_model->dataInfo(["dynamic_id" => $input["dynamic_id"], "user_id" => $this->_user_id]);
        if(!empty($find) && $find["status"] == 1) {
            $res = $this->thumbs_model->dataUpdate(["id" => $find["id"]], ["status" => -1]);
        } elseif(!empty($find)) {
            $res = $this->thumbs_model->dataUpdate(["id" => $find["id"]], ["status" => 1, "create_time" => time(), "have_look" => 0]);
        } else {
            $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->_user_id, "dynamic_id" => $input["dynamic_id"], "coach_id" => $dynamic["coach_id"]];
            $res = $this->thumbs_model->dataAdd($insert);
        }
        return $this->success($res);
    }
    public function getFollowData()
    {
        if(!$this->getUserId()) {
            $arr["coach_status"] = 0;
            $arr["num"] = $arr["coach_status"];
            return $this->success($arr);
        }
        $follow_model = new \app\dynamic\model\DynamicFollow();
        $dis = [];
        if($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $dis[] = ["d.industry_type", "<>", 1];
        }
        $arr["num"] = $follow_model->getFollowDynamicNum($this->getUserId(), $dis);
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "=", 2];
        $coach_model = new \app\massage\model\Coach();
        $cap_info = $coach_model->where($cap_dis)->find();
        $arr["coach_status"] = !empty($cap_info) ? 1 : 0;
        return $this->success($arr);
    }
    public function followCoachList()
    {
        $input = $this->_param;
        $follow_model = new \app\dynamic\model\DynamicFollow();
        $shield_model = new \app\massage\model\ShieldList();
        $coach_id = $shield_model->where(["user_id" => $this->getUserId()])->where("type", "in", [2, 3])->column("coach_id");
        $dis[] = ["a.coach_id", "not in", $coach_id];
        $dis[] = ["a.status", "=", 1];
        $dis[] = ["b.status", "=", 2];
        $dis[] = ["a.user_id", "=", $this->getUserId()];
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((b.lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((b.lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (b.lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
        $data = $follow_model->followCoachList($dis, $alh);
        if(!empty($data["data"])) {
            $comm_model = new \app\massage\model\Order();
            foreach ($data["data"] as &$v) {
                $num = $comm_model->where(["coach_id" => $v["coach_id"], "pay_type" => 7])->count();
                $v["order_num"] += $num;
                $v["distance"] = distance_text($v["distance"]);
                $v["fans_num"] = $follow_model->where(["status" => 1, "coach_id" => $v["coach_id"]])->count();
                $v["fans_num"] = getFriendNum($v["fans_num"]);
                $v["order_num"] = getFriendNum($v["order_num"]);
            }
        }
        return $this->success($data);
    }
}

?>